package com.ext.aic.utils;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.ext.aic.AICApplication;
import com.ext.aic.box.RegisterAccessibilityService;
import com.ext.aic.box.RegisterNotificationService;
import com.eyinfo.android_pure_utils.HandlerManager;
import com.eyinfo.android_pure_utils.events.RunnableParamsN;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

public class ServiceUtils {

    private static volatile ServiceUtils instance;

    public static ServiceUtils getInstance() {
        if (instance == null) {
            synchronized (ServiceUtils.class) {
                if (instance == null) {
                    instance = new ServiceUtils();
                }
            }
        }
        return instance;
    }

    public void startNotificationService(Context context) {
        String serviceName = RegisterNotificationService.class.getName();
        if (!ObjectJudge.isServiceRunning(context, serviceName)
                && !PermissionUtils.getInstance().isBackgroundRestricted(context)) {
            Intent serviceIntent = new Intent(context, RegisterNotificationService.class);
            context.startForegroundService(serviceIntent);
        }
    }

    /**
     * 自动启动服务
     * 1、当通知权限已经开启、通知服务未运行、未被厂商限制为后台运行时，在应用启动同时自启动通知服务
     * 2、当无障碍权限已经开启、无障碍服务未运行、未被厂商限制为后台运行时，在应用启动后延迟1秒启动无障碍服务
     * 3、在1或2各自满足的情况下，同时启用前台服务
     *
     * @param delay 是否延迟启动
     */
    public void autoStartService(boolean delay) {
        if (delay) {
            HandlerManager.getInstance().postDelayed(new RunnableParamsN<Object>() {
                @Override
                public void run(Object... objects) {
                    startNecessaryService();
                }
            }, 1000);
        } else {
            startNecessaryService();
        }
    }

    private void startNecessaryService() {
        Context applicationContext = AICApplication.getInstance().getApplicationContext();
        if (PermissionUtils.getInstance().isBackgroundRestricted(applicationContext)) {
            return;
        }
//        if (PermissionUtils.getInstance().isNotificationEnabled(applicationContext)) {
//            String serviceName = RegisterNotificationService.class.getName();
//            if (!ObjectJudge.isServiceRunning(applicationContext, serviceName)) {
//                startNotificationService(applicationContext);
//                startForegroundService(applicationContext, RegisterAccessibilityService.class);
//            }
//        }
        if (PermissionUtils.getInstance().isAccessibilityServiceEnabled(applicationContext)) {
            String serviceName = RegisterAccessibilityService.class.getName();
            if (!ObjectJudge.isServiceRunning(applicationContext, serviceName)) {
                startAccessibilityService(applicationContext);
                startForegroundService(applicationContext, RegisterAccessibilityService.class);
            }
        }
    }

    public void startAccessibilityService(Context context) {
        if (!PermissionUtils.getInstance().isAccessibilityServiceEnabled(context)) {
            Intent intent = new Intent(context, RegisterAccessibilityService.class);
            context.startService(intent);
        }
    }

    /**
     * 启动前台服务
     *
     * @param context 上下文
     * @param clazz   服务类
     * @param <S>
     */
    public <S extends Service> void startForegroundService(Context context, Class<S> clazz) {
        String serviceName = clazz.getName();
        if (!ObjectJudge.isServiceRunning(context, serviceName)
                && !PermissionUtils.getInstance().isBackgroundRestricted(context)) {
            Intent serviceIntent = new Intent(context, clazz);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent);
            } else {
                context.startService(serviceIntent);
            }
        }
    }
}
